<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xmlhttp.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xmlhttp.js.html">xmlhttp.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Low level handling of XMLHttpRequest.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.net.XmlHttp&#39;);
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>/**
<a name="line24"></a> * Factory class for creating XMLHttpRequest objects.
<a name="line25"></a> * @return {XMLHttpRequest|GearsHttpRequest} A new XMLHttpRequest object.
<a name="line26"></a> */
<a name="line27"></a>goog.net.XmlHttp = function() {
<a name="line28"></a>  return goog.net.XmlHttp.factory_();
<a name="line29"></a>};
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Gets the options to use with the XMLHttpRequest object from the factory.
<a name="line34"></a> * @return {Object} The options.
<a name="line35"></a> */
<a name="line36"></a>goog.net.XmlHttp.getOptions = function() {
<a name="line37"></a>  return goog.net.XmlHttp.cachedOptions_ ||
<a name="line38"></a>         (goog.net.XmlHttp.cachedOptions_ = goog.net.XmlHttp.optionsFactory_());
<a name="line39"></a>};
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * The factory for creating XMLHttpRequest objets.
<a name="line44"></a> * @type {Function?}
<a name="line45"></a> * @private
<a name="line46"></a> */
<a name="line47"></a>goog.net.XmlHttp.factory_ = null;
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * The factory for creating the Options for the XMLHttpRequest objets given
<a name="line52"></a> * from the factory.
<a name="line53"></a> * @type {Function?}
<a name="line54"></a> * @private
<a name="line55"></a> */
<a name="line56"></a>goog.net.XmlHttp.optionsFactory_ = null;
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * The cached options object used to minimize object allocations.
<a name="line61"></a> * @type {Object?}
<a name="line62"></a> * @private
<a name="line63"></a> */
<a name="line64"></a>goog.net.XmlHttp.cachedOptions_ = null;
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Sets the factories for creating XMLHttpRequest objects and their options.
<a name="line69"></a> * @param {Function} factory The factory for XMLHttpRequest objects.
<a name="line70"></a> * @param {Function} optionsFactory The factory for options.
<a name="line71"></a> */
<a name="line72"></a>goog.net.XmlHttp.setFactory = function(factory, optionsFactory) {
<a name="line73"></a>  goog.net.XmlHttp.factory_ = factory;
<a name="line74"></a>  goog.net.XmlHttp.optionsFactory_ = optionsFactory;
<a name="line75"></a>
<a name="line76"></a>  // Clear the cached options.
<a name="line77"></a>  goog.net.XmlHttp.cachedOptions_ = null;
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Default factory class for creating XMLHttpRequest objects.
<a name="line83"></a> * @return {XMLHttpRequest} A new XMLHttpRequest object.
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.net.XmlHttp.defaultFactory_ = function() {
<a name="line87"></a>  var progId = goog.net.XmlHttp.getProgId_();
<a name="line88"></a>  if (progId) {
<a name="line89"></a>    return new ActiveXObject(progId);
<a name="line90"></a>  } else {
<a name="line91"></a>    return new XMLHttpRequest();
<a name="line92"></a>  }
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Default factory class for creating the options.
<a name="line98"></a> * @return {Object} The options.
<a name="line99"></a> * @private
<a name="line100"></a> */
<a name="line101"></a>goog.net.XmlHttp.defaultOptionsFactory_ = function() {
<a name="line102"></a>  var progId = goog.net.XmlHttp.getProgId_();
<a name="line103"></a>  var options = {};
<a name="line104"></a>  if (progId) {
<a name="line105"></a>    options[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] = true;
<a name="line106"></a>    options[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] = true;
<a name="line107"></a>  }
<a name="line108"></a>  return options;
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>// Set the default factories.
<a name="line113"></a>goog.net.XmlHttp.setFactory(
<a name="line114"></a>    goog.net.XmlHttp.defaultFactory_, goog.net.XmlHttp.defaultOptionsFactory_);
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Type of options that an XmlHttp object can have.
<a name="line119"></a> * @enum {number}
<a name="line120"></a> */
<a name="line121"></a>goog.net.XmlHttp.OptionType = {
<a name="line122"></a>  /**
<a name="line123"></a>   * Whether a goog.nullFunction should be used to clear the onreadystatechange
<a name="line124"></a>   * handler instead of null.
<a name="line125"></a>   */
<a name="line126"></a>  USE_NULL_FUNCTION: 0,
<a name="line127"></a>
<a name="line128"></a>  /**
<a name="line129"></a>   * NOTE: In IE if send() errors on a *local* request the readystate
<a name="line130"></a>   * is still changed to COMPLETE.  We need to ignore it and allow the
<a name="line131"></a>   * try/catch around send() to pick up the error.
<a name="line132"></a>   */
<a name="line133"></a>  LOCAL_REQUEST_ERROR: 1
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Status constants for XMLHTTP, matches:
<a name="line139"></a> * http://msdn.microsoft.com/library/default.asp?url=/library/
<a name="line140"></a> *   en-us/xmlsdk/html/0e6a34e4-f90c-489d-acff-cb44242fafc6.asp
<a name="line141"></a> * @enum {number}
<a name="line142"></a> */
<a name="line143"></a>goog.net.XmlHttp.ReadyState = {
<a name="line144"></a>  /**
<a name="line145"></a>   * Constant for when xmlhttprequest.readyState is uninitialized
<a name="line146"></a>   */
<a name="line147"></a>  UNINITIALIZED: 0,
<a name="line148"></a>
<a name="line149"></a>  /**
<a name="line150"></a>   * Constant for when xmlhttprequest.readyState is loading.
<a name="line151"></a>   */
<a name="line152"></a>  LOADING: 1,
<a name="line153"></a>
<a name="line154"></a>  /**
<a name="line155"></a>   * Constant for when xmlhttprequest.readyState is loaded.
<a name="line156"></a>   */
<a name="line157"></a>  LOADED: 2,
<a name="line158"></a>
<a name="line159"></a>  /**
<a name="line160"></a>   * Constant for when xmlhttprequest.readyState is in an interactive state.
<a name="line161"></a>   */
<a name="line162"></a>  INTERACTIVE: 3,
<a name="line163"></a>
<a name="line164"></a>  /**
<a name="line165"></a>   * Constant for when xmlhttprequest.readyState is completed
<a name="line166"></a>   */
<a name="line167"></a>  COMPLETE: 4
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * The ActiveX PROG ID string to use to create xhr&#39;s in IE. Lazily initialized.
<a name="line173"></a> * @type {string?}
<a name="line174"></a> * @private
<a name="line175"></a> */
<a name="line176"></a>goog.net.XmlHttp.ieProgId_ = null;
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Initialize the private state used by other functions.
<a name="line181"></a> * @return {string} The ActiveX PROG ID string to use to create xhr&#39;s in IE.
<a name="line182"></a> * @private
<a name="line183"></a> */
<a name="line184"></a>goog.net.XmlHttp.getProgId_ = function() {
<a name="line185"></a>  // The following blog post describes what PROG IDs to use to create the
<a name="line186"></a>  // XMLHTTP object in Internet Explorer:
<a name="line187"></a>  // http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
<a name="line188"></a>  // However we do not (yet) fully trust that this will be OK for old versions
<a name="line189"></a>  // of IE on Win9x so we therefore keep the last 2.
<a name="line190"></a>  if (!goog.net.XmlHttp.ieProgId_ &amp;&amp; typeof XMLHttpRequest == &#39;undefined&#39; &amp;&amp;
<a name="line191"></a>      typeof ActiveXObject != &#39;undefined&#39;) {
<a name="line192"></a>    // Candidate Active X types.
<a name="line193"></a>    var ACTIVE_X_IDENTS = [&#39;MSXML2.XMLHTTP.6.0&#39;, &#39;MSXML2.XMLHTTP.3.0&#39;,
<a name="line194"></a>                           &#39;MSXML2.XMLHTTP&#39;, &#39;Microsoft.XMLHTTP&#39;];
<a name="line195"></a>    for (var i = 0; i &lt; ACTIVE_X_IDENTS.length; i++) {
<a name="line196"></a>      var candidate = ACTIVE_X_IDENTS[i];
<a name="line197"></a>      /** @preserveTry */
<a name="line198"></a>      try {
<a name="line199"></a>        new ActiveXObject(candidate);
<a name="line200"></a>        // NOTE: cannot assign progid and return candidate in one line
<a name="line201"></a>        // because JSCompiler complaings: BUG 658126
<a name="line202"></a>        goog.net.XmlHttp.ieProgId_ = candidate;
<a name="line203"></a>        return candidate;
<a name="line204"></a>      } catch (e) {
<a name="line205"></a>        // do nothing; try next choice
<a name="line206"></a>      }
<a name="line207"></a>    }
<a name="line208"></a>
<a name="line209"></a>    // couldn&#39;t find any matches
<a name="line210"></a>    throw Error(&#39;Could not create ActiveXObject. ActiveX might be disabled,&#39; +
<a name="line211"></a>                &#39; or MSXML might not be installed&#39;);
<a name="line212"></a>  }
<a name="line213"></a>
<a name="line214"></a>  return /** @type {string} */ (goog.net.XmlHttp.ieProgId_);
<a name="line215"></a>};
</pre>


</body>
</html>
